<?php $this->_extends('../_layouts/default_layout'); ?>
<?php $this->_block('contents'); ?>
<script type="text/javascript" src="<?=Q::ini('custom_system/base_url')?>js/jq.mselect.js?"></script>
<script type="text/javascript">
$(function(){
	var product_categories_data = <?=$product_categories?>;
	$(document).mselect(product_categories_data, 'product_categories_id', 'product_categories_id', "<?=isset($_GET['product_categories_id'])?$_GET['product_categories_id']:''?>");
	$('.datepicker').datepicker({dateFormat:'yy-mm-dd',changeMonth:true});
})
</script>
<fieldset><legend>查询条件</legend>
<form method="get">
<? include(Q::ini('custom_system/elements_dir') . 'searchform_element.php');?>
<table class="form_table">
<tr>
	<td></td>
	<th>日期</th>
	<td>
		<input type="text" name="begin_time" class="datepicker" value="<?=isset($_GET['begin_time'])?$_GET['begin_time']:date('Y-m-01',CURRENT_TIMESTAMP)?>" /> to
		<input type="text" name="end_time" class="datepicker" value="<?=isset($_GET['end_time'])?$_GET['end_time']:date('Y-m-d', CURRENT_TIMESTAMP)?>" />
	</td>
</tr>
<tr>
	<td><input type="checkbox" name="by_product_categories" value="1" <?if(isset($_GET['by_product_categories'])&&$_GET['by_product_categories'])echo "checked='checked'"?>  /></td>
	<th>采购分类</th>
	<td><input type="hidden" name="product_categories_id" id="product_categories_id" /></td>
</tr>
<tr>
	<td></td>
	<th>仓库</th>
	<td>
		<select name="warehouse_id">
			<option value="">请选择..</option>
			<?foreach ($warehouse as $w):?>
			<option value="<?=$w['id']?>" <?if (isset($_GET['warehouse_id']) && $_GET['warehouse_id'] == $w['id'])echo "selected='selected'"?>><?=$w['name']?></option>
			<?endforeach;?>
		</select>
	</td>
</tr>
<tr>
	<td></td>
	<th>周期</th>
	<td>
		<label><input name="period" type="radio" value="daily" <?=(isset($_GET['period'])&&$_GET['period']=='daily')?"checked='checked'":''?> />日报表</label>&nbsp;&nbsp;
		<label><input name="period" type="radio" value="weekly" <?=(isset($_GET['period'])&&$_GET['period']=='weekly')?"checked='checked'":''?> />周报表</label>&nbsp;&nbsp;
		<label><input name="period" type="radio" value="monthly" <?=((isset($_GET['period'])&&$_GET['period']=='monthly')||(!isset($_GET['period'])))?"checked='checked'":''?> />月报表</label>
	</td>
</tr>
<tr>
	<td></td>
	<td><input type="hidden" name="is_search" value="1" /></td>
	<td><Input type="submit" class="button" value="查询" /></td>
</tr>
</table>
</form>
</fieldset>
<?if (isset($result)):?>
<?$in_quantity = $in_cost = $out_quantity = $out_cost = $balance_quantity = $balance_cost = 0;?>
<table width="100%" class="list_table">
<?if (empty($_GET['by_product_categories'])):?>
<tr>
	<th rowspan="2" colspan="2">日期</th>
	<th rowspan="2">仓库</th>
	<th colspan="2">期初</th>
	<th colspan="2">入库</th>
	<th colspan="2">出库</th>
	<th colspan="2">结存</th>
</tr>
<tr>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
</tr>
<?foreach ($result as $r):?>
<tr>
	<td colspan="2"><?=($_GET['period']=='daily')?date('Y-m-d', strtotime($r['begin_time'])):(($_GET['period']=='weekly')?'第'.date('W',strtotime($r['begin_time'])).'周':date('Y-m',strtotime($r['begin_time'])))?></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
</tr>
<?foreach ($r['warehouse'] as $rw):?>
<?php
	$in_quantity += $rw['in_quantity'];
	$in_cost += $rw['in_cost'];
	$out_quantity += $rw['out_quantity'];
	$out_cost += $rw['out_cost'];
?>
<tr>
	<td colspan="2"></td>
	<td><?=$rw['warehouse_name']?></td>
	<td><?=$rw['init_quantity']?></td>
	<td><?=sprintf('%.2f', $rw['init_cost'])?></td>
	<td>
		<?if ($rw['in_quantity']):?>
		<table width="100%" class="list_table">
			<?foreach ($rw['type']['in'] as $k => $i):?>
			<?if ($i['in_quantity']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=$i['in_quantity']?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=$rw['in_quantity']?></td>
			</tr>
		</table>
		<?else:?>
		<?=$rw['in_quantity']?>
		<?endif;?>
	</td>
	<td>
		<?if ($rw['in_cost']):?>
		<table width="100%" class="list_table">
			<?foreach ($rw['type']['in'] as $k => $i):?>
			<?if ($i['in_cost']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=sprintf('%.2f', $i['in_cost'])?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=sprintf('%.2f', $rw['in_cost'])?></td>
			</tr>
		</table>
		<?else:?>
		<?=sprintf('%.2f', $rw['in_cost'])?>
		<?endif;?>
	</td>
	<td>
		<?if ($rw['out_quantity']):?>
		<table width="100%" class="list_table">
			<?foreach ($rw['type']['out'] as $k => $o):?>
			<?if ($o['out_quantity']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=$o['out_quantity']?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=$rw['out_quantity']?></td>
			</tr>
		</table>
		<?else:?>
		<?=$rw['out_quantity']?>
		<?endif;?>
	</td>
	<td>
		<?if ($rw['out_cost']):?>
		<table width="100%" class="list_table">
			<?foreach ($rw['type']['out'] as $k => $o):?>
			<?if ($o['out_cost']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=sprintf('%.2f', $o['out_cost'])?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=sprintf('%.2f', $rw['out_cost'])?></td>
			</tr>
		</table>
		<?else:?>
		<?=$rw['out_cost']?>
		<?endif;?>
	</td>
	<td><?=$rw['balance_quantity']?></td>
	<td><?=sprintf('%.2f', $rw['balance_cost'])?></td>
</tr>
<?$balance_quantity += $rw['balance_quantity'];?>
<?$balance_cost += $rw['balance_cost'];?>
<?endforeach;?>
<?endforeach;?>
<tr>
	<td align="right" colspan="3"><b>总计：</b></td>
	<td><b><?=$balance_quantity+$out_quantity-$in_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $balance_cost+$out_cost-$in_cost)?></b></td>
	<td><b><?=$in_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $in_cost)?></b></td>
	<td><b><?=$out_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $out_cost)?></b></td>
	<td><b><?=$balance_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $balance_cost)?></b></td>
</tr>
<?endif;?>

<?if (isset($_GET['by_product_categories'])):?>
<tr>
	<th rowspan="2" colspan="2">日期</th>
	<th rowspan="2">仓库</th>
	<th rowspan="2">分类</th>
	<th colspan="2">期初</th>
	<th colspan="2">入库</th>
	<th colspan="2">出库</th>
	<th colspan="2">结存</th>
</tr>
<tr>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
	<th>数量</th>
	<th>成本</th>
</tr>
<?foreach ($result as $rr):?>
<tr>
	<td colspan="2"><?=($_GET['period']=='daily')?date('Y-m-d', strtotime($rr['begin_time'])):(($_GET['period']=='weekly')?'第'.date('W',strtotime($rr['begin_time'])).'周':date('Y-m',strtotime($rr['begin_time'])))?></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
</tr>
<?if (isset($rr['warehouse'])):?>
<?foreach ($rr['warehouse'] as $rw):?>
<tr>
	<td colspan="2"></td>
	<td><?=$rw['warehouse_name']?></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
	<td></td>
</tr>
<?if (isset($rw['categories']) && $rw['categories']):?>
<?foreach ($rw['categories'] as $r):?>
<?php
	$in_quantity += $r['in_quantity'];
	$in_cost += $r['in_cost'];
	$out_quantity += $r['out_quantity'];
	$out_cost += $r['out_cost'];
?>
<tr>
	<td colspan="2"></td>
	<td></td>
	<td><?=$r['categories_name']?></td>
	<td><?=$r['init_quantity']?></td>
	<td><?=sprintf('%.2f', $r['init_cost'])?></td>
	<td>
		<?if ($r['in_quantity']):?>
		<table width="100%" class="list_table">
			<?foreach ($r['type']['in'] as $k => $i):?>
			<?if ($i['in_quantity']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=$i['in_quantity']?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=$r['in_quantity']?></td>
			</tr>
		</table>
		<?else:?>
		<?=$r['in_quantity']?>
		<?endif;?>
	</td>
	<td>
		<?if ($r['in_cost']):?>
		<table width="100%" class="list_table">
			<?foreach ($r['type']['in'] as $k => $i):?>
			<?if ($i['in_cost']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=sprintf('%.2f', $i['in_cost'])?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=sprintf('%.2f', $r['in_cost'])?></td>
			</tr>
		</table>
		<?else:?>
		<?=sprintf('%.2f', $r['in_cost'])?>
		<?endif;?>
	</td>
	<td>
		<?if ($r['out_quantity']):?>
		<table width="100%" class="list_table">
			<?foreach ($r['type']['out'] as $k => $o):?>
			<?if ($o['out_quantity']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=$o['out_quantity']?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=$r['out_quantity']?></td>
			</tr>
		</table>
		<?else:?>
		<?=$r['out_quantity']?>
		<?endif;?>
	</td>
	<td>
		<?if ($r['out_cost']):?>
		<table width="100%" class="list_table">
			<?foreach ($r['type']['out'] as $k => $o):?>
			<?if ($o['out_cost']):?>
			<tr>
				<th width="80"><?=$k?></th>
				<td><?=sprintf('%.2f', $o['out_cost'])?></td>
			</tr>
			<?endif;?>
			<?endforeach;?>
			<tr>
				<th width="80">小计</th>
				<td><?=sprintf('%.2f', $r['out_cost'])?></td>
			</tr>
		</table>
		<?else:?>
		<?=sprintf('%.2f', $r['out_cost'])?>
		<?endif;?>
	</td>
	<td><?=$r['balance_quantity']?></td>
	<td><?=sprintf('%.2f', $r['balance_cost'])?></td>
</tr>
<?endforeach;?>
<?endif;?>
<?if (isset($rw['categories']) && $rw['categories']):?>
<?foreach ($rw['categories'] as $r):?>
<?$balance_quantity += $r['balance_quantity'];?>
<?$balance_cost += $r['balance_cost'];?>
<?endforeach;?>
<?endif;?>
<?endforeach;?>
<?endif;?>
<?endforeach;?>
<tr>
	<td colspan="4"><b>总计：</b></td>
	<td><b><?=$balance_quantity+$out_quantity-$in_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $balance_cost+$out_cost-$in_cost)?></b></td>
	<td><b><?=$in_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $in_cost)?></b></td>
	<td><b><?=$out_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $out_cost)?></b></td>
	<td><b><?=$balance_quantity?></b></td>
	<td><b><?=sprintf('%.2f', $balance_cost)?></b></td>
</tr>
<?endif;?>
</table>
<?endif;?>
<?php $this->_endblock();?>